﻿

#region Usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Serialization;



#endregion

namespace ZN.Common {

    /// <summary>
    /// The CBO class generates objects.
    /// </summary>
    /// -----------------------------------------------------------------------------
    public class CBO {

        #region "Private Constants"

        private const string defaultPrimaryKey = "ItemID";

        private const string objectMapCacheKey = "ObjectMap_";

        #endregion

        #region "Private Shared Methods"

        #region "Object Creation/Hydration Helper Methods"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CreateObjectFromReader creates an object of a specified type from the
        /// provided DataReader
        /// </summary>
        /// <param name="objType">The type of the Object</param>
        /// <param name="dr">The IDataReader to use to fill the object</param>
        /// <param name="closeReader">A flag that indicates whether the DataReader should be closed</param>
        /// <returns>The object (TObject)</returns>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static object CreateObjectFromReader(Type objType, IDataReader dr, bool closeReader) {
            object objObject = null;
            bool isSuccess = Null.NullBoolean;
            bool canRead = true;

            if (closeReader) {
                canRead = false;
                //read datareader
                if (dr.Read()) {
                    canRead = true;
                }
            }
            try {
                if (canRead) {
                    //Create the Object
                    objObject = CreateObject(objType, false);

                    //hydrate the custom business object
                    FillObjectFromReader(objObject, dr);
                }
                isSuccess = true;
            } finally {
                //Ensure DataReader is closed
                if ((!isSuccess)) {
                    closeReader = true;
                }
                CloseDataReader(dr, closeReader);
            }
            return objObject;
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillListFromReader fills a list of objects of a specified type 
        /// from a DataReader
        /// </summary>
        /// <param name="objType">The type of the business object</param>
        /// <param name="dr">The IDataReader to use to fill the objects</param>
        /// <param name="objList">The List to Fill</param>
        /// <param name="closeReader">A flag that indicates whether the DataReader should be closed</param>
        /// <returns>A List of objects (TItem)</returns>
        /// <remarks></remarks>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static IList FillListFromReader(Type objType, IDataReader dr, IList objList, bool closeReader) {
            object objObject;
            bool isSuccess = Null.NullBoolean;
            try {
                //iterate datareader
                while (dr.Read()) {
                    //Create the Object
                    objObject = CreateObjectFromReader(objType, dr, false);
                    //add to collection
                    objList.Add(objObject);
                }
                isSuccess = true;
            } finally {
                //Ensure DataReader is closed
                if ((!isSuccess)) {
                    closeReader = true;
                }
                CloseDataReader(dr, closeReader);
            }
            return objList;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillListFromReader fills a list of objects of a specified type
        /// from a DataReader
        /// </summary>
        /// <param name="dr">The IDataReader to use to fill the objects</param>
        /// <param name="objList">The List to Fill</param>
        /// <param name="closeReader">A flag that indicates whether the DataReader should be closed</param>
        /// <returns>A List of objects (TItem)</returns>
        /// <remarks></remarks>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static IList<TItem> FillListFromReader<TItem>(IDataReader dr, IList<TItem> objList, bool closeReader) {
            TItem objObject;
            bool isSuccess = Null.NullBoolean;
            try {
                //iterate datareader
                while (dr.Read()) {
                    //Create the Object
                    objObject = (TItem)CreateObjectFromReader(typeof(TItem), dr, false);
                    //add to collection
                    objList.Add(objObject);
                }
                isSuccess = true;
            } finally {
                //Ensure DataReader is closed
                if ((!isSuccess)) {
                    closeReader = true;
                }
                CloseDataReader(dr, closeReader);
            }
            return objList;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillObjectFromReader fills an object from the provided DataReader.  If the object 
        /// implements the IHydratable interface it will use the object's IHydratable.Fill() method.
        /// Otherwise, it will use reflection to fill the object.
        /// </summary>
        /// <param name="objObject">The object to fill</param>
        /// <param name="dr">The DataReader</param>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static void FillObjectFromReader(object objObject, IDataReader dr) {
            try {

                //Use Reflection
                HydrateObject(objObject, dr);

            } catch (IndexOutOfRangeException iex) {
                throw;
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// HydrateObject uses reflection to hydrate an object.
        /// </summary>
        /// <param name="hydratedObject">The object to Hydrate</param>
        /// <param name="dr">The IDataReader that contains the columns of data for the object</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static void HydrateObject(object hydratedObject, IDataReader dr) {
            PropertyInfo objPropertyInfo = null;
            Type propType = null;
            object coloumnValue;
            Type objDataType;
            int intIndex;
            //get cached object mapping for type
            ObjectMappingInfo objMappingInfo = GetObjectMapping(hydratedObject.GetType());

            //fill object with values from datareader
            for (intIndex = 0; intIndex <= dr.FieldCount - 1; intIndex++) {
                //If the Column matches a Property in the Object Map's PropertyInfo Dictionary
                if (objMappingInfo.Properties.TryGetValue(dr.GetName(intIndex).ToUpperInvariant(), out objPropertyInfo)) {
                    //Get its type
                    propType = objPropertyInfo.PropertyType;
                    //If property can be set
                    if (objPropertyInfo.CanWrite) {
                        //Get the Data Value from the data reader
                        coloumnValue = dr.GetValue(intIndex);
                        //Get the Data Value's type
                        objDataType = coloumnValue.GetType();
                        if (coloumnValue == null || coloumnValue == DBNull.Value) {
                            //set property value to Null
                            objPropertyInfo.SetValue(hydratedObject, Null.SetNull(objPropertyInfo), null);
                        } else if (propType.Equals(objDataType)) {
                            //Property and data objects are the same type
                            objPropertyInfo.SetValue(hydratedObject, coloumnValue, null);
                        } else {
                            //business object info class member data type does not match datareader member data type
                            //need to handle enumeration conversions differently than other base types
                            if (propType.BaseType.Equals(typeof(Enum))) {
                                //check if value is numeric and if not convert to integer ( supports databases like Oracle )
                                if (Regex.IsMatch(coloumnValue.ToString(), "^\\d+$")) {
                                    objPropertyInfo.SetValue(hydratedObject, Enum.ToObject(propType, Convert.ToInt32(coloumnValue)), null);
                                } else {
                                    objPropertyInfo.SetValue(hydratedObject, Enum.ToObject(propType, coloumnValue), null);
                                }
                            } else if (propType == typeof(Guid)) {
                                //guid is not a datatype common across all databases ( ie. Oracle )
                                objPropertyInfo.SetValue(hydratedObject, Convert.ChangeType(new Guid(coloumnValue.ToString()), propType), null);
                            } else if (propType == typeof(Version)) {
                                objPropertyInfo.SetValue(hydratedObject, new Version(coloumnValue.ToString()), null);
                            } else if (coloumnValue is IConvertible) {
                                if (coloumnValue!=null&&propType==typeof(bool)) {//edit by yyj
                                    coloumnValue=coloumnValue.ToString()=="1"?true:false;
                                }
                                objPropertyInfo.SetValue(hydratedObject, Convert.ChangeType(coloumnValue, propType), null);
                            } else {
                                // try explicit conversion
                                objPropertyInfo.SetValue(hydratedObject, coloumnValue, null);
                            }
                        }
                    }
                }
            }
        }

        #endregion

        #region "Object Mapping Helper Methods"



        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetColumnName gets the name of the Database Column that maps to the property.
        /// </summary>
        /// <param name="objProperty">The proeprty of the business object</param>
        /// <returns>The name of the Database Column</returns>
        /// <history>
        /// 	[cnurse]	12/02/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static string GetColumnName(PropertyInfo objProperty) {
            string columnName = objProperty.Name;
            return columnName;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetObjectMapping gets an instance of the ObjectMappingInfo class for the type.
        /// This is cached using a high priority as reflection is expensive.
        /// </summary>
        /// <param name="objType">The type of the business object</param>
        /// <returns>An ObjectMappingInfo object representing the mapping for the object</returns>
        /// <history>
        /// 	[cnurse]	12/01/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static ObjectMappingInfo GetObjectMapping(Type objType) {

            //Create an ObjectMappingInfo instance
            var objMap = new ObjectMappingInfo();
            objMap.ObjectType = objType.FullName;
            //Reflect on class to create Object Map
            objMap.PrimaryKey = GetPrimaryKey(objType);
            objMap.TableName = GetTableName(objType);
            //Iterate through the objects properties and add each one to the ObjectMappingInfo's Properties Dictionary 
            foreach (PropertyInfo objProperty in objType.GetProperties()) {
                objMap.Properties.Add(objProperty.Name.ToUpperInvariant(), objProperty);
                objMap.ColumnNames.Add(objProperty.Name.ToUpperInvariant(), GetColumnName(objProperty));
            }


            //Return Object Map
            return objMap;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetPrimaryKey gets the Primary Key property
        /// </summary>
        /// <param name="objType">The type of the business object</param>
        /// <returns>The name of the Primary Key property</returns>
        /// <history>
        /// 	[cnurse]	12/01/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static string GetPrimaryKey(Type objType) {
            string primaryKey = defaultPrimaryKey;
            return primaryKey;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// GetTableName gets the name of the Database Table that maps to the object.
        /// </summary>
        /// <param name="objType">The type of the business object</param>
        /// <returns>The name of the Database Table</returns>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private static string GetTableName(Type objType) {
            string tableName = string.Empty;
            //If no attrubute then use Type Name
            if (string.IsNullOrEmpty(tableName)) {
                tableName = objType.Name;
                if (tableName.EndsWith("Info")) {
                    //Remove Info ending
                    tableName.Replace("Info", string.Empty);
                }
            }

            return tableName;
        }

        #endregion

        #endregion

        #region "Public Shared Methods"

        #region "Clone Object"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CloneObject clones an object
        /// </summary>
        /// <param name="objObject">The Object to Clone</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static object CloneObject(object objObject) {
            try {
                Type objType = objObject.GetType();
                object objNewObject = Activator.CreateInstance(objType);
                //get cached object mapping for type
                ObjectMappingInfo objMappingInfo = GetObjectMapping(objType);
                foreach (KeyValuePair<string, PropertyInfo> kvp in objMappingInfo.Properties) {
                    PropertyInfo objProperty = kvp.Value;
                    if (objProperty.CanWrite) {
                        //Check if property is ICloneable
                        var objPropertyClone = objProperty.GetValue(objObject, null) as ICloneable;
                        if (objPropertyClone == null) {
                            objProperty.SetValue(objNewObject, objProperty.GetValue(objObject, null), null);
                        } else {
                            objProperty.SetValue(objNewObject, objPropertyClone.Clone(), null);
                        }
                        //Check if Property is IEnumerable
                        var enumerable = objProperty.GetValue(objObject, null) as IEnumerable;
                        if (enumerable != null) {
                            var list = objProperty.GetValue(objNewObject, null) as IList;
                            if (list != null) {
                                foreach (object obj in enumerable) {
                                    list.Add(CloneObject(obj));
                                }
                            }
                            var dic = objProperty.GetValue(objNewObject, null) as IDictionary;
                            if (dic != null) {
                                foreach (DictionaryEntry de in enumerable) {
                                    dic.Add(de.Key, CloneObject(de.Value));
                                }
                            }
                        }
                    }
                }
                return objNewObject;
            } catch (Exception exc) {
                throw;
                return null;
            }
        }

        #endregion

        #region "CloseDataReader"

        public static void CloseDataReader(IDataReader dr, bool closeReader) {
            //close datareader
            if (dr != null && closeReader) {
                dr.Close();
            }
        }

        #endregion

        #region "Create Object"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CreateObject creates a new object of Type TObject.
        /// </summary>
        /// <typeparam name="TObject">The type of object to create.</typeparam>
        /// <remarks>This overload does not initialise the object</remarks>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static TObject CreateObject<TObject>() {
            return (TObject)CreateObject(typeof(TObject), false);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CreateObject creates a new object of Type TObject.
        /// </summary>
        /// <typeparam name="TObject">The type of object to create.</typeparam>
        /// <param name="initialise">A flag that indicates whether to initialise the
        /// object.</param>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static TObject CreateObject<TObject>(bool initialise) {
            return (TObject)CreateObject(typeof(TObject), initialise);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// CreateObject creates a new object.
        /// </summary>
        /// <param name="objType">The type of object to create.</param>
        /// <param name="initialise">A flag that indicates whether to initialise the
        /// object.</param>
        /// <history>
        /// 	[cnurse]	11/30/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static object CreateObject(Type objType, bool initialise) {
            object objObject = Activator.CreateInstance(objType);

            if (initialise) {
                InitializeObject(objObject);
            }
            return objObject;
        }

        #endregion

        #region "DeserializeObject"

        public static TObject DeserializeObject<TObject>(string fileName) {
            return DeserializeObject<TObject>(XmlReader.Create(new FileStream(fileName, FileMode.Open, FileAccess.Read)));
        }

        public static TObject DeserializeObject<TObject>(XmlDocument document) {
            return DeserializeObject<TObject>(XmlReader.Create(new StringReader(document.OuterXml)));
        }

        public static TObject DeserializeObject<TObject>(Stream stream) {
            return DeserializeObject<TObject>(XmlReader.Create(stream));
        }

        public static TObject DeserializeObject<TObject>(TextReader reader) {
            return DeserializeObject<TObject>(XmlReader.Create(reader));
        }

        public static TObject DeserializeObject<TObject>(XmlReader reader) {
            //First Create the Object
            var objObject = CreateObject<TObject>(true);
            //Try to cast the Object as IXmlSerializable
            var xmlSerializableObject = objObject as IXmlSerializable;
            if (xmlSerializableObject == null) {
                //Use XmlSerializer
                var serializer = new XmlSerializer(objObject.GetType());
                objObject = (TObject)serializer.Deserialize(reader);
            } else {
                //Use XmlReader
                xmlSerializableObject.ReadXml(reader);
            }
            return objObject;
        }

        #endregion

        #region "FillCollection"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillCollection fills a Collection of objects from a DataReader
        /// </summary>
        /// <param name="dr">The Data Reader</param>
        /// <param name="objType">The type of the Object</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static ArrayList FillCollection(IDataReader dr, Type objType) {
            return (ArrayList)FillListFromReader(objType, dr, new ArrayList(), true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillCollection fills a Collection of objects from a DataReader
        /// </summary>
        /// <param name="dr">The Data Reader</param>
        /// <param name="objType">The type of the Object</param>
        /// <param name="closeReader">Flag that indicates whether the Data Reader should be closed.</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static ArrayList FillCollection(IDataReader dr, Type objType, bool closeReader) {
            return (ArrayList)FillListFromReader(objType, dr, new ArrayList(), closeReader);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillCollection fills a Collection of objects from a DataReader
        /// </summary>
        /// <param name="dr">The Data Reader</param>
        /// <param name="objType">The type of the Object</param>
        /// <param name="objToFill">An IList to fill</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IList FillCollection(IDataReader dr, Type objType, ref IList objToFill) {
            return FillListFromReader(objType, dr, objToFill, true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillCollection fills a Collection of objects from a DataReader
        /// </summary>
        /// <typeparam name="TItem">The type of object</typeparam>
        /// <param name="dr">The Data Reader</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<TItem> FillCollection<TItem>(IDataReader dr) {
            return (List<TItem>)FillListFromReader(dr, new List<TItem>(), true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillCollection fills a Collection of objects from a DataReader
        /// </summary>
        /// <typeparam name="TItem">The type of object</typeparam>
        /// <param name="objToFill">The List to fill</param>
        /// <param name="dr">The Data Reader</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IList<TItem> FillCollection<TItem>(IDataReader dr, ref IList<TItem> objToFill) {
            return FillListFromReader(dr, objToFill, true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillCollection fills a List of objects from a DataReader
        /// </summary>
        /// <typeparam name="TItem">The type of the Object</typeparam>
        /// <param name="objToFill">The List to fill</param>
        /// <param name="dr">The Data Reader</param>
        /// <param name="closeReader">A flag that indicates whether the DataReader should be closed</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static IList<TItem> FillCollection<TItem>(IDataReader dr, IList<TItem> objToFill, bool closeReader) {
            return FillListFromReader(dr, objToFill, closeReader);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Generic version of FillCollection fills a List custom business object of a specified type 
        /// from the supplied DataReader
        /// </summary>
        /// <param name="dr">The IDataReader to use to fill the object</param>
        /// <param name="objType">The type of the Object</param>
        /// <param name="totalRecords">The total No of records</param>
        /// <returns>A List of custom business objects</returns>
        /// <remarks></remarks>
        /// <history>
        /// 	[cnurse]	01/28/2008	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static ArrayList FillCollection(IDataReader dr, ref Type objType, ref int totalRecords) {
            var objFillCollection = (ArrayList)FillListFromReader(objType, dr, new ArrayList(), false);
            try {
                if (dr.NextResult()) {
                    //Get the total no of records from the second result
                    totalRecords = Utils.GetTotalRecords(ref dr);
                }
            } catch (Exception exc) {
                throw;
            } finally {
                //Ensure DataReader is closed
                CloseDataReader(dr, true);
            }
            return objFillCollection;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Generic version of FillCollection fills a List custom business object of a specified type 
        /// from the supplied DataReader
        /// </summary>
        /// <typeparam name="T">The type of the business object</typeparam>
        /// <param name="dr">The IDataReader to use to fill the object</param>
        /// <param name="totalRecords"></param>
        /// <returns>A List of custom business objects</returns>
        /// <remarks></remarks>
        /// <history>
        /// 	[cnurse]	10/10/2005	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static List<T> FillCollection<T>(IDataReader dr, ref int totalRecords) {
            IList<T> objFillCollection = FillCollection(dr, new List<T>(), false);
            try {
                if (dr.NextResult()) {
                    //Get the total no of records from the second result
                    totalRecords = Utils.GetTotalRecords(ref dr);
                }
            } catch (Exception exc) {
                throw;
            } finally {
                //Ensure DataReader is closed
                CloseDataReader(dr, true);
            }
            return (List<T>)objFillCollection;
        }

        #endregion

        #region "FillObject"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillObject fills an object from a DataReader
        /// </summary>
        /// <typeparam name="TObject">The type of the object</typeparam>
        /// <param name="dr">The Data Reader</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static TObject FillObject<TObject>(IDataReader dr) {
            return (TObject)CreateObjectFromReader(typeof(TObject), dr, true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillObject fills an object from a DataReader
        /// </summary>
        /// <typeparam name="TObject">The type of the object</typeparam>
        /// <param name="dr">The Data Reader</param>
        /// <param name="closeReader">A flag that indicates the reader should be closed</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static TObject FillObject<TObject>(IDataReader dr, bool closeReader) {
            return (TObject)CreateObjectFromReader(typeof(TObject), dr, closeReader);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillObject fills an object from a DataReader
        /// </summary>
        /// <param name="dr">The Data Reader</param>
        /// <param name="objType">The type of the object</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static object FillObject(IDataReader dr, Type objType) {
            return CreateObjectFromReader(objType, dr, true);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// FillObject fills an object from a DataReader
        /// </summary>
        /// <param name="dr">The Data Reader</param>
        /// <param name="objType">The type of the object</param>
        /// <param name="closeReader">A flag that indicates the reader should be closed</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Documented
        /// </history>
        /// -----------------------------------------------------------------------------
        public static object FillObject(IDataReader dr, Type objType, bool closeReader) {
            return CreateObjectFromReader(objType, dr, closeReader);
        }

        #endregion

        public static IQueryable<TItem> FillQueryable<TItem>(IDataReader dr) {
            return FillListFromReader(dr, new List<TItem>(), true).AsQueryable();
        }

        #region "InitializeObject"

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// InitializeObject initialises all the properties of an object to their 
        /// Null Values.
        /// </summary>
        /// <param name="objObject">The object to Initialise</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static void InitializeObject(object objObject) {
            //initialize properties
            foreach (PropertyInfo objPropertyInfo in GetObjectMapping(objObject.GetType()).Properties.Values) {
                if (objPropertyInfo.CanWrite) {
                    objPropertyInfo.SetValue(objObject, Null.SetNull(objPropertyInfo), null);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// InitializeObject initialises all the properties of an object to their 
        /// Null Values.
        /// </summary>
        /// <param name="objObject">The object to Initialise</param>
        /// <param name="objType">The type of the object</param>
        /// <history>
        /// 	[cnurse]	11/29/2007	Created
        /// </history>
        /// -----------------------------------------------------------------------------
        public static object InitializeObject(object objObject, Type objType) {
            //initialize properties
            foreach (PropertyInfo objPropertyInfo in GetObjectMapping(objType).Properties.Values) {
                if (objPropertyInfo.CanWrite) {
                    objPropertyInfo.SetValue(objObject, Null.SetNull(objPropertyInfo), null);
                }
            }
            return objObject;
        }

        #endregion

        #endregion

    }
}
